package com.zadtek.sharkRider.activity.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.zadtek.sharkRider.R;
import com.zadtek.sharkRider.activity.OrderDetailActivity_;
import com.zadtek.sharkRider.activity.data.OrderoverOrd;

/**
 * 已完成订单-总订单
 * @author Administrator
 *
 */
public class OrderoverAdapter extends BaseAdapter {

	private Context context;
	/**
	 * 显示数据的集合
	 */
	ArrayList<OrderoverOrd> cdlistdata;
	Intent intent;

	public OrderoverAdapter(Context context, ArrayList list) {
		this.context = context;
		this.cdlistdata = list;
	}

	@Override
	public int getCount() {
		return cdlistdata != null ? cdlistdata.size() : 0;
	}

	@Override
	public Object getItem(int position) {
		return cdlistdata.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		if (cdlistdata != null) {
			ViewHolder holder = null;
			if (convertView == null) {
				convertView = LayoutInflater.from(context).inflate(
						R.layout.order_over_dayitem, null);
				holder = new ViewHolder();

				holder.mainord = (TextView) convertView
						.findViewById(R.id.mainord);
				holder.psmoney = (TextView) convertView
						.findViewById(R.id.psmoney);
				holder.detailname = (TextView) convertView
						.findViewById(R.id.detailname);
				holder.address = (TextView) convertView
						.findViewById(R.id.address);
				convertView.setTag(holder);

			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			if (convertView != null) {
				if (cdlistdata.size() > position) {
					holder.mainord
							.setText("总订单号："+cdlistdata.get(position).getMain_id());
					holder.psmoney.setText("合计"+cdlistdata.get(position).getReturnpay()+"元");
					String sitelist = cdlistdata.get(position).getSitelist();
					sitelist = sitelist.replaceAll(",", "\n");
					holder.detailname.setText(sitelist);
					holder.address.setText(cdlistdata.get(position).getSendaddress());
					
					convertView.setOnClickListener(new OnClickListener() {
						@Override
						public void onClick(View arg0) {
							// TODO Auto-generated method stub
							Intent intent = new Intent(context,
									OrderDetailActivity_.class);
							intent.putExtra("mainid", cdlistdata.get(position)
									.getMain_id());
							intent.putExtra("status", "over");
							intent.putExtra("address", cdlistdata.get(0).getSendaddress());
							context.startActivity(intent);
						}
					});
				}
			}
		}
		return convertView;
	}

	public static class ViewHolder {
		TextView mainord;
		TextView psmoney;
		TextView detailname;	//取货店铺名称
		TextView address;	//送货地址
	}

}
